package ulmermark.demo.concurrency;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;

import ulmermark.demo.concurrency.core.AbstractResource;
import ulmermark.demo.concurrency.core.Resource;
import ulmermark.demo.concurrency.core.Target;
import ulmermark.demo.concurrency.core.TimerResource;
import ulmermark.demo.concurrency.core.inbox.InboxFactory;


public class SampleResource extends AbstractResource implements Resource<SampleResourceConfigParam>{
	private static final Logger LOGGER = Logger.getLogger( SampleResource.class);
	private final TimerResource timer;
	
	public SampleResource( String name, final InboxFactory inboxFactory, 
							final TimerResource timer){
		super( name, inboxFactory);
		this.timer = timer;
	}
	
	public void onAppInit() {
	}

	public void start() {
		final Target<DateTime> t = new Target<DateTime>( this.getInbox()){

			@Override
			public void received(final DateTime message) {
				LOGGER.info("Timer fired at " + message);
			}
			
		};
		
		this.timer.scheduleAtFixedRate("Timer1", t, 100, TimeUnit.MILLISECONDS);
		this.timer.scheduleAtFixedRate("Timer2", t, 100, TimeUnit.MILLISECONDS);
		this.timer.scheduleAtFixedRate("Timer3", t, 100, TimeUnit.MILLISECONDS);
		this.timer.scheduleAtFixedRate("Timer4", t, 100, TimeUnit.MILLISECONDS);
		this.timer.scheduleAtFixedRate("Timer5", t, 100, TimeUnit.MILLISECONDS);
		this.timer.scheduleAtFixedRate("Timer6", t, 100, TimeUnit.MILLISECONDS);
		
	}

	public void subscribeToTargets() {
	}

	@Override
	public SampleResourceConfigParam getConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void configure(SampleResourceConfigParam configParam) {
		// TODO Auto-generated method stub
		
	}

}
